package cn.ml.mybatisdemo;

import cn.ml.mybatisdemo.dao.StudentMapper;
import cn.ml.mybatisdemo.entity.Student;
import cn.ml.mybatisdemo.entity.StudentExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.util.List;

public class TestStudent {
    public static void main(String[] args) throws IOException {

        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader("mybatis.cfg.xml"));
        SqlSession sqlSession = sqlSessionFactory.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

        System.out.println("************分页插件**************");
        PageHelper.startPage(1,5);
        StudentExample example = new StudentExample();
        StudentExample.Criteria criteria = example.createCriteria(); //创建查询条件
        criteria.andGenderEqualTo("男");
        List<Student> students = mapper.selectByExample(example);
        PageInfo<Student> pageInfo = new PageInfo<>(students);

        for (Student s:students){
            System.out.println(s);
        }
        System.out.println("结果总页数为："+pageInfo.getPages());
        System.out.println("结果总条数为："+pageInfo.getTotal());
        /*System.out.println("************添加**************");
        Student student = new Student();
        student.setId(27);
        student.setName("撒贝宁");
        //student.setGender("男");
        mapper.updateByPrimaryKey(student);
        sqlSession.commit();*/



    }
}
